/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import utils.HibernateUtil;
import pojo.Loaiphim;

/**
 *
 * @author Hnmtuan
 */
public class LoaiPhimDAO {

    public static SessionFactory _localSessionFactory;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static List<Loaiphim> getLoaiPhims() throws Exception {
        List<Loaiphim> items = null;
        Session localSession = null;
        String localQuery = "FROM Loaiphim p WHERE p.trangThai = 1";
        try {
            localSession = _localSessionFactory.openSession();
            localSession.beginTransaction();
            items = localSession.createQuery(localQuery).list();
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Loaiphim> getAllLoaiPhims() throws Exception {
        List<Loaiphim> items = null;
        Session localSession = null;
        String localQuery = "FROM Loaiphim p";
        try {
            localSession = _localSessionFactory.openSession();
            localSession.beginTransaction();
            items = localSession.createQuery(localQuery).list();
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Loaiphim> getLoaiPhimsByCodePhim(String codePhim) throws Exception {
        List<Loaiphim> items = null;
        Session localSession = null;
        String localQuery = "FROM Loaiphim lp WHERE lp.CodeLoaiPhim = :codePhim AND lp.TrangThai = :statusPhim";
        try {
            // Step 1: validate input params
            if (codePhim.isEmpty()) {
                throw new Exception("Param not valid");
            }
            // Step 02 : create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            // Step 03 : begin retrieving transaction in above session.
            localSession.beginTransaction();
            // Step 04 : set parameters and retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setString("codePhim", codePhim);
            q.setInteger("statusPhim", 1);
            items = q.list();
            // Step 05 : commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhimsByCodePhim", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                // Step 06 : close retrieving session
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getLoaiPhimsByCodePhim", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static boolean insertLoaiPhims(Loaiphim paramLoaiPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            // Step 1: validate input params
            if (paramLoaiPhims == null) {
                throw new Exception("Param not valid");
            }
            // Step 02 : create insert session.
            localSession = _localSessionFactory.openSession();
            // Step 03 : begin insert transaction in above session.
            localSession.beginTransaction();
            // Step 04 : save object into database.
            localSession.save(paramLoaiPhims);
            // Step 05 : commit and end insert transaction in above  session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "insertLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                // Step 06 : close insert session
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "insertLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updateLoaiPhims(Loaiphim paramLoaiPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            // Step 1: validate input params
            if (paramLoaiPhims == null) {
                throw new Exception("Param not valid");
            }
            // Step 02 : create update session.
            localSession = _localSessionFactory.openSession();
            // Step 03 : begin update transaction in above session.
            localSession.beginTransaction();
            // Step 04 : update object into database.
            localSession.update(paramLoaiPhims);
            // Step 05 : commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "updateLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                // Step 06 : close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "updateLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteLoaiPhims(Loaiphim paramLoaiPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            // Step 1: validate input params
            if (paramLoaiPhims == null) {
                throw new Exception("Param not valid");
            }
            // Step 02 : create delete session.
            localSession = _localSessionFactory.openSession();
            // Step 03 : begin delete transaction in above session.
            localSession.beginTransaction();
            // Step 04 : delete object into database.
            localSession.delete(paramLoaiPhims);
            // Step 05 : commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "deleteLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                // Step 06 : close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "deleteLoaiPhims", LoaiPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
